﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BullsAndCows
{
    /// <summary>
    /// 猜数字游戏计算方式
    /// </summary>
    public class CalcHelper
    {
        /// <summary>
        /// 数字不能重复
        /// </summary>
        /// <param name="secret"></param>
        /// <param name="guess"></param>
        /// <returns></returns>
        public static string GetHintByNotSame (string guess)
        {
            string result = "";
            var bulls = 0;
            var cows = 0;
            string secret = GetCodesByNoSame(4);
            for (int i = 0; i < secret.Length; i++)
            {
                if (secret[i] == guess[i])
                {
                    bulls++;
                }
                else
                {
                    if (secret.Contains(guess[i]))
                    {
                        cows++;
                    }
                }
            }
            result = string.Format("{0}A{1}B", bulls, cows);
            return result;
        }
        /// <summary>
        /// 允许存在相同的数字
        /// </summary>
        /// <param name="secret"></param>
        /// <param name="guess"></param>
        /// <returns></returns>
        public static string GetHintByHasSame( string guess)
        {
            string secret = GetCodesBySame(4);
            var tag = new int[secret.Length];
            string result = "";
            var bulls = 0;
            var cows = 0;

            var secretCopy = secret;
            for (int i = 0; i < secret.Length; i++)
            {
                if (secret[i] == guess[i])
                {
                    bulls++;
                    if (tag[i] == 1)
                    {
                        cows--;
                    }
                    tag[i] = 1;
                    secretCopy = ChangeStr(secretCopy, i);
                }
                else
                {
                    var index = secretCopy.IndexOf(guess[i]);

                    if (index > -1)
                    {
                        if (tag[index] == 1)
                        {
                            continue;
                        }
                        cows++;
                        tag[index] = 1;
                        secretCopy = ChangeStr(secretCopy, index);
                    }
                }
            }
            result = string.Format("{0}A{1}B", bulls, cows);

            return result;

        }
        private static string ChangeStr(string secretCopy, int index)
        {
            var temp = "";
            temp = secretCopy.Substring(0, index) + "A";
            if (index + 1 != secretCopy.Length)
            {
                temp += secretCopy.Substring(index + 1, secretCopy.Length - index - 1);
            }
            secretCopy = temp;
            return secretCopy;
        }
/// <summary>
/// 生成一个不重复的随机码
/// </summary>
/// <param name="rmNum">随机数位数</param>
/// <returns></returns>
        private static string GetCodesByNoSame(int rmNum)
        {
            string random = "";
            Random rm = new Random();

            for (int i = 0; random.Length < rmNum; i++)
            {
                int nValue = rm.Next(10);
                if (!random.Contains(nValue.ToString()))
                {
                    random += nValue;
                    Console.WriteLine(nValue.ToString());
                }
            }
            return random;

        }
        /// <summary>
        /// 生成一个可重复的随机码
        /// </summary>
        /// <param name="rmNum">随机数位数</param>
        /// <returns></returns>
        public static string GetCodesBySame(int rmNum)
        {
            Random rm = new Random();
            var end = "";
            end = end.PadLeft(rmNum, '9');
            var endInt = int.Parse(end);
            int nValue = rm.Next(10 * rmNum, endInt);
            return nValue.ToString();

        }
    }
}
